/**
 * Created with CLion.
 * Description: 
 * User: loong
 * Date: 2024/9/2
 * Time: 21:31
 */

#ifndef GROUPSERVICE_H
#define GROUPSERVICE_H

#include <Group.grpc.pb.h>
#include "IDataHandle.h"

/*struct MemberInfo {
    std::string name;
    std::string id;
};
using MemberInfoSPtr = std::shared_ptr<MemberInfo>;

struct GroupInfo {
    std::string name;
    std::string id;
    MemberInfoSPtr creator;

    enum Type
    {
        Creator,
        Admin,
        Normal
    };

    // std::map<std::string, MemberInfoSPtr> managers;
    // std::map<std::string, MemberInfoSPtr> members;
    std::map<std::string,std::pair<Type,MemberInfoSPtr>> members;
};

using GroupInfoSPtr = std::shared_ptr<GroupInfo>;*/

class GroupService final : public Chat::GroupService::Service
{
public:
    GroupService() = default;

    ~GroupService() override = default;

    void SetDataHandle(const IDataHandleSPtr & handle);

    grpc::Status SearchGroup(grpc::ServerContext * context, const Chat::SearchGroupRequest * request, Chat::SearchGroupReply * response) override;

    grpc::Status CreateGroup(grpc::ServerContext * context, const Chat::CreateGroupRequest * request, Chat::CreateGroupReply * response) override;

    grpc::Status JoinGroup(grpc::ServerContext * context, const Chat::JoinGroupRequest * request, Chat::CommonGroupReply * response) override;

    grpc::Status InviteJoinGroup(grpc::ServerContext * context, const Chat::InviteJoinGroupRequest * request, Chat::CommonGroupReply * response) override;

    grpc::Status ExitGroup(grpc::ServerContext * context, const Chat::ExitGroupRequest * request, Chat::CommonGroupReply * response) override;

    grpc::Status KickOut(grpc::ServerContext * context, const Chat::KickOutRequest * request, Chat::CommonGroupReply * response) override;

    grpc::Status DisbandGroup(grpc::ServerContext * context, const Chat::DisbandGroupRequest * request, Chat::CommonGroupReply * response) override;

private:
    IDataHandleSPtr handle_{nullptr};

#if 0//Test
    bool CheckGroupExistsWithId(const std::string & id)
    {
        return all_group_.count(id) > 0;
    };

    bool CheckGroupExistsWithName(const std::string & name)
    {
        for(auto & [id, info] : all_group_)
            if(info->name == name)
                return true;
        return false;
    };

    void AddGroup(const GroupInfoSPtr & info)
    {
        all_group_[info->id] = info;
    }

    void RemoveGroup(const std::string & id)
    {
        all_group_.erase(id);
    }

    void GroupAddMember(const std::string & id, const MemberInfoSPtr & info)
    {
        all_group_[id]->members.insert({info->id, info});
    }

    void GroupRemoveMember(const std::string & group_id, const std::string & member_id)
    {
        all_group_[group_id]->members.erase(member_id);
    }

    void AddMember(const MemberInfoSPtr & info)
    {
        all_members_[info->id] = info;
    }

    void removeMember(const std::string & id)
    {
        all_members_.erase(id);
    }


private:
    // std::map<std::string, GroupInfoSPtr>  all_group_;  //<id,GroupInfoSPtr>
    // std::map<std::string, MemberInfoSPtr> all_members_;//<id,MemberInfoSPtr>
#endif
};

#endif//GROUPSERVICE_H
